/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upf.grupE.Controller;

import edu.upf.grupE.DDBB.DDBBAction;
import edu.upf.grupE.DDBB.DDBBActionResult;
import edu.upf.grupE.DDBB.DDBBManager;
import edu.upf.grupE.Entities.Video;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.ValidationException;

/**
 *
 * @author Madhava Carrillo <madhava at me.com>
 */
public class VideoController {

    static DDBBManager gestorBD;
    static DDBBActionResult resultadoBD;
    static DDBBAction dbaction;

    public static long createOrUpdate(Video updated) {
        long id = 0;
        try {
            gestorBD = DDBBManager.getInstance();
            StringWriter xml = new StringWriter();
            Marshaller marshaller = new Marshaller(xml);
            dbaction = null;

            if (!updated.hasId()) {
                updated.setId(0);
                marshaller.marshal(updated);
                String myXmlString = xml.toString();
                myXmlString = myXmlString.substring(myXmlString.indexOf("?>") + 2);
                myXmlString = myXmlString.substring(myXmlString.indexOf(">") + 1);
                dbaction = new DDBBAction("Video.insertByIdAuto", new String[]{myXmlString});
            } else if (VideoController.getById(updated.getId()) != null) {
                marshaller.marshal(updated);
                String myXmlString = xml.toString();
                myXmlString = myXmlString.substring(myXmlString.indexOf("?>") + 2);
                dbaction = new DDBBAction("Video.updateById", new String[]{updated.getId() + "", myXmlString});
            } else {
                marshaller.marshal(updated);
                String myXmlString = xml.toString();
                myXmlString = myXmlString.substring(myXmlString.indexOf("?>") + 2);
                dbaction = new DDBBAction("Video.insert", new String[]{myXmlString});
            }
            resultadoBD = gestorBD.execute(dbaction);
            //dbaction = new DDBBAction("Video.getById", new String[]{updated.getId() + ""});
            //resultadoBD = gestorBD.execute(dbaction);
            if (resultadoBD.isOk()) {
                id = updated.getId();
            } else {
                if (resultadoBD.getException() instanceof org.exolab.castor.xml.MarshalException) {
                    String resultado = (String) resultadoBD.getResult();
                    resultado = resultado.substring(resultado.indexOf(">") + 1, resultado.indexOf("</"));
                    System.out.println(resultado);
                    id = Long.parseLong(resultado);
                }
            }

        //id = (Long) result.get(0);
        } catch (MarshalException ex) {
            Logger.getLogger(VideoController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ValidationException ex) {
            Logger.getLogger(VideoController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(VideoController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return id;
    }

    public static Video getById(Long id) {
        try {
            gestorBD = DDBBManager.getInstance();
        } catch (IOException ex) {
            Logger.getLogger(PictureController.class.getName()).log(Level.SEVERE, null, ex);
        }
        dbaction = new DDBBAction("Video.getById", new String[]{id + ""});
        resultadoBD = gestorBD.execute(dbaction);
        Video pic = null;
        if (resultadoBD.isOk()) {
            List result = (List) resultadoBD.getResult();
            if (!result.isEmpty()) {
                pic = (Video) result.get(0);
            }
        }
        return pic;
    }

    public static void delById(Long id) {
        try {
            gestorBD = DDBBManager.getInstance();
        } catch (IOException ex) {
            Logger.getLogger(PictureController.class.getName()).log(Level.SEVERE, null, ex);
        }
        dbaction = new DDBBAction("Video.deleteById", new String[]{id + ""});
        resultadoBD = gestorBD.execute(dbaction);
    }


    public static Vector<Video> getAll() {
        Vector<Video> alb = null;
        try {
            gestorBD = DDBBManager.getInstance();
            dbaction = new DDBBAction("Video.getAll", new String[]{});
            resultadoBD = gestorBD.execute(dbaction);

            if (resultadoBD.isOk()) {
                List result = (List) resultadoBD.getResult();
                alb = (Vector<Video>) result;
            }

        } catch (IOException ex) {
            Logger.getLogger(AlbumController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return alb;
    }
}
